<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :before-close="handlerBefore"
    :visible.sync="visible"
  >
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="128px">
      <el-form-item label="模块名称" prop="moduleName">
        <el-input v-model="dataForm.moduleName" placeholder="模块名称"></el-input>
      </el-form-item>
      <el-form-item label="模块路径(h5使用)" prop="modulePath">
        <el-input v-model="dataForm.modulePath" placeholder="模块路径(h5使用)"></el-input>
      </el-form-item>
      <el-form-item label="模块class(android)" prop="moduleClass">
        <el-input v-model="dataForm.moduleClass" placeholder="模块class(android)"></el-input>
      </el-form-item>
      <el-form-item label="是否需要参数" prop="moduleClass">
        <el-switch v-model="dataForm.isParams" active-color="#13ce66" inactive-color="#999999"></el-switch>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
      <el-form-item label="参数" prop="params">
        <el-input v-model="dataForm.params" placeholder="参数"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handlerBefore()">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="loading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      codeEditor: '',
      loading: false,
      dataForm: {
        id: 0,
        moduleName: '',
        moduleClass: '',
        modulePath: '',
        sort: 1,
        params: '',
        isParams: false
      },
      dataRule: {
        paramValue: [
          { required: true, message: '参数值不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handlerBefore() {
      if (this.codeEditor) {
        this.codeEditor.destroy()
      }
      this.visible = false
    },
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: '/module/info',
            method: 'post',
            data: { id: this.dataForm.id }
          }).then(data => {
            if (data && data.code === 200) {
              this.dataForm.moduleName = data.data.moduleName
              this.dataForm.moduleClass = data.data.moduleClass
              this.dataForm.modulePath = data.data.modulePath
              this.dataForm.sort = data.data.sort
              this.dataForm.params = data.data.params
              this.dataForm.isParams = data.data.isParams == 1 ? true : false
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.loading = true
          this.$http({
            url: `/module/${!this.dataForm.id ? 'save' : 'update'}`,
            method: 'post',
            data: {
              id: this.dataForm.id || undefined,
              moduleName: this.dataForm.moduleName,
              modulePath: this.dataForm.modulePath,
              moduleClass: this.dataForm.moduleClass,
              sort: this.dataForm.sort,
              params: this.dataForm.params,
              isParams: this.dataForm.isParams ? '1' : '0'
            }
          }).then(data => {
            console.log(this.dataForm.params)
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.visible = false
              this.$emit('refreshDataList')
              this.loading = false
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
